package com.banmao.algorithm.No1To100.No3_Longest_Substring_Without_Repeating_Characters;

import java.util.HashMap;

class Solution01 {
    static int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) return 0;
        HashMap<Character, Integer> hmap = new HashMap<>();
        int max = 0;
        int min = 0;
        for (int i = 0; i < s.length(); i++) {
            if (hmap.containsKey(s.charAt(i))) {
                min = Math.max(min, hmap.get(s.charAt(i)) + 1);
            }
            hmap.put(s.charAt(i), i);
            max = Math.max(max, i - min + 1);
        }
        return max;
    }
}
